//
//  ViewController.m
//  23-QQ音乐
//
//  Created by dllo on 16/8/15.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "QQ_MVTableViewCell.h"
#import "AFNetworking.h"
#import "MVModel.h"
#import "MVData.h"
#import "MVContents.h"
#import "MVTableList.h"
#import "MVConllectionList.h"

#define kTableViewCellReuse @"reuse"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *qq_MVTableView;
@property (nonatomic, strong) MVModel *mvModel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleData];
}
- (void)handleData {
    AFHTTPSessionManager *Session = [AFHTTPSessionManager manager];
    NSString *URL = @"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&format=jsonp&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294&jsonpCallback=jsonp1";
    Session.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [Session GET:URL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@", responseObject);
        NSData *data = responseObject;
        NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        dataString = [dataString stringByReplacingOccurrencesOfString:@"jsonp1(" withString:@""];
        dataString = [dataString substringToIndex:(dataString.length - 1)];
//        NSLog(@"%@", dataString);
        NSData *finalData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:finalData options:NSJSONReadingMutableContainers error:&error];
//        NSLog(@"%@", result);
        self.mvModel = [MVModel modelWithDic:result];
        [_qq_MVTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 30)];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 30)];
    CGFloat centerX = CGRectGetWidth(tableView.bounds) / 2;
    label.center = CGPointMake(centerX, 15);
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"最新";
    [backView addSubview:label];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(CGRectGetWidth(tableView.bounds) - 40, 0, 30, 30);
    [button setImage:[UIImage imageNamed:@"qianwang"] forState:UIControlStateNormal];
    [backView addSubview:button];
    backView.backgroundColor = [UIColor whiteColor];
    return backView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return _mvModel.data.contents.list.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QQ_MVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    
    cell.mvListArr = self.mvModel.data.contents.list[indexPath.section].mvlist;
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
